package com.ww.springboot.es.dto;

import com.ww.springboot.es.en.ForecastStatusEnum;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author ZH500
 * @date : 2020/11/18 19:34
 */
//@Document(indexName = "ww_test_#{forecastConfiguration.profile}", shards = 3, replicas = 2)
@Document(indexName = "ctms_forecast_log_2022_test", shards = 3, replicas = 2)
@Data
public class ForecastRecord implements Serializable {


    @Id
    private String id;


    /**
     * 业务单号  可能值 订单号/配货单号(包裹号)/跟踪号/扩展跟踪号
     */
    @Field(type = FieldType.Keyword)
    private String bizNumber;


    /**
     * 业务类型
     */
    @Field(type = FieldType.Keyword)
    private Integer bizType;

    /**
     * 业务类型
     */
    private String bizTypeName;


    /**
     * 请求方系统名称
     */
    @Field(type = FieldType.Keyword)
    private String requestSysName;


    /**
     * 响应方 系统名称
     */
    @Field(type = FieldType.Keyword)
    private String responseSysName;


    /**
     * 请求报文
     */
    private String requestMessage;


    /**
     * 响应报文
     */
    private String responseMessage;


    /**
     * 业务状态
     * 1 处理成功
     * 2 处理失败
     */
    @Field(type = FieldType.Integer)
    private Integer status;


    /**
     * 关联MQ消息对象
     */
    private String mqMessage;

    /**
     * 关联MQ queue
     */
    private String consumerQueue;


    /**
     * 请求次数
     */
    private Integer count;


    /**
     * 創建时间
     */
    @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second_millis)
    private LocalDateTime createTime;


    /**
     * 修改时间
     */
    @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second_millis)
    private LocalDateTime updateTime;


    /**
     * 租户id
     */
    @Field(type = FieldType.Keyword)
    private String tenantId;


    /**
     * 异常信息
     */
    private String errorMsg;


    public ForecastRecord(boolean isTenantId, String requestMessage, String responseMessage, Integer status) {
        this.requestMessage = requestMessage;
        this.responseMessage = responseMessage;
        this.status = status;
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
        this.count = 0;
    }

    public ForecastRecord(boolean isTenantId) {
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
        this.count = 0;
        //默认值设为success
        this.status = ForecastStatusEnum.CODE_0.getCode();
    }

    public ForecastRecord(boolean isTenantId, String requestMessage, String errorMsg) {
        this.requestMessage = requestMessage;
        this.status = ForecastStatusEnum.CODE_1.getCode();
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
        this.count = 0;
        this.errorMsg = errorMsg;
    }

    public ForecastRecord() {
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
        this.count = 0;
        //默认值设为success
        this.status = ForecastStatusEnum.CODE_0.getCode();
    }


    public Boolean isSuccess() {
        return this.status.equals(ForecastStatusEnum.CODE_0.getCode());
    }
}
